<?php
if ( !defined('IN_ADMINCP') || !defined('IN_SEC') ){
	exit('Access Denied.');
}

$adminid = $admincp->adminid;
$group = $admincp->groups[$admincp->admin['groupid']];

$id = abs(intval($_G['gp_id']));

if( $id && !$customer = fetch('customer', $id) ){
	$admincp->cpmsg('客户不存在');
}
$companys = get_parse_companys();

$status = customer_status();
$demands = customer_demand();
$sources = customer_source();
$natures = customer_nature();
$jobs = customer_jobs();

$provinces = get_province();
$citys = get_city($customer['province']);

if( $customer ){
	if( $customer['adminid'] ){
		if( !$admincp->manage && $adminid != $customer['adminid'] ){
			$admincp->cpmsg('您无权查看该客户信息');
		}
	}
	$customer['demands'] = is_string($customer['demands']) ? json_decode($customer['demands'], true) : array();
	$customer_demands = array();
	foreach( $customer['demands'] as $k => $v ){
		$customer_demands[$k] = $demands[$k];
	}
}

$admins = array();
foreach( $admincp->admins as $k => $v ){
	$admins[$v['id']] = array(
		'id' => $v['id'],
		'name' => heivrid($v)
	);
}

if( $customer && $_G['gp_detail'] == 'yes' && !is_post() ){
	$contact_types = contact_types();
	$customer_contacts = limitquery('customer_contacts', array('customerid' => $customer['id']), array('order' => 'ORDER BY `create_time` DESC'));

	$contacts = array();

	foreach( $customer_contacts as $k => $v ){
		$date = date('Y-m-d', $v['create_time']);
		$contacts[$date][$v['id']] = $v;
	}

	$select_2_maps = array();

	foreach( $jobs as $k => $v ){
		$select_2_maps['jobs'][] = array(
			'id' => $k,
			'text' => $v
		);
	}
	foreach( $natures as $k => $v ){
		$select_2_maps['natures'][] = array(
			'id' => $k,
			'text' => $v
		);
	}
	foreach( $status as $k => $v ){
		$select_2_maps['status'][] = array(
			'id' => $k,
			'text' => $v
		);
	}
	foreach( $demands as $k => $v ){
		$select_2_maps['demands'][] = array(
			'id' => $k,
			'text' => $v
		);
	}
	foreach( $provinces as $k => $v ){
		$select_2_maps['provinces'][] = array(
			'id' => $v['id'],
			'text' => $v['name']
		);
	}
	$select_2_maps['citys'] = array();
	foreach( $citys as $k => $v ){
		$select_2_maps['citys'][] = array(
			'id' => $v['id'],
			'text' => $v['name']
		);
	}

	$select_2_maps['demands_values'] = array_keys($customer['demands']);

	$contact_count = DB::Count('customer_contacts', array('customerid' => $customer['id']));
	$last_month = strtotime("last Month");
	$contact_count_by_month = DB::Count('customer_contacts', array('customerid' => $customer['id'], "`create_time` > {$last_month}"));
	$customer['province_name'] = get_province_name($customer['province']);
	$customer['city_name'] = get_city_name($customer['city']);
	$customer_logs = limitquery('customer_log', array('customerid' => $customer['id']), array('order' => 'ORDER BY `create_time` DESC'));
	$customer_teams = limitquery('team', array('customerid' => $customer['id']), array('order' => 'ORDER BY `create_time` DESC'));
	$tmpname = '_detail';
}
if( is_post() && !$_G['gp_act'] ){
	if( !confirm_password($admincp->admin) ){
		$admincp->cpmsg('密码不正确！授权操作失败！');
	}
	$name = $_G['gp_name'] ? $_G['gp_name'] : $admincp->cpmsg('公司名称不能为空！');
	$nature = $_G['gp_nature'] ? $_G['gp_nature'] : '';
	$status = $_G['gp_status'] ? $_G['gp_status'] : 0;
	$demand = $_G['gp_demand'] ? $_G['gp_demand'] : array();
	$source = $_G['gp_source'] ? $_G['gp_source'] : '';
	$url = $_G['gp_url'] ? $_G['gp_url'] : '';
	$address = $_G['gp_address'] ? $_G['gp_address'] : '';
	$detail = $_G['gp_detail'] ? htmlspecialchars($_G['gp_detail']) : '';

	$contact_name = $_G['gp_contact_name'];
	$contact_job = $_G['gp_contact_job'];
	$contact_tel = $_G['gp_contact_tel'];
	$contact_phone = $_G['gp_contact_phone'];
	$contact_email = $_G['gp_contact_email'];
	$contact_qq = $_G['gp_contact_qq'];
	$next_contact_time = $_G['gp_next_contact_time'] ? strtotime($_G['gp_next_contact_time']) : 0;

	//2015.10.26添加合伙人账号密码设置
	/*$account = $_G['gp_account'];
	$pass = $_G['gp_pass'];
	if($account && $pass){
		$remark = 3;
	}else{
		$remark = 2;
	}*/

	$adminid = $_G['gp_adminid'] ? $_G['gp_adminid'] : ($admincp->manage ? 0 : $adminid);
	$province = abs(intval($_G['gp_province']));
	$city = abs(intval($_G['gp_city']));


	$update = array(
		'province' => $province,
		'city' => $city,
		'name' => $name,
		'nature' => $nature,
		'status' => $status,
		'source' => $source,
		'url' => $url,
		'address' => $address,
		'adminid' => $adminid,
		'detail' => $detail,
		'demands' => json_encode($demand),
		'contact_name' => $contact_name,
		'contact_job' => $contact_job,
		'contact_tel' => $contact_tel,
		'contact_phone' => $contact_phone,
		'contact_email' => $contact_email,
		'contact_qq' => $contact_qq,
		'next_contact_time' => $next_contact_time,
		'update_time' => SYS_TIME,
		/*'account' => $account,
		'pass' => $pass,
		'remark' => $remark*/
	);
	$customerid = $customer['id'];
	$customer_log = array(
		'customerid' => $customerid,
		'adminid' => $admincp->adminid,
		'create_time' => SYS_TIME
	);
	if( $customer ){
		update('customer', $customer['id'], $update);
		$admincp->log('customer', $customerid, '编辑客户：' . $name, $update);

		if( $adminid != $customer['adminid'] ){
			if( $customer['adminid'] ){
				if( !$adminid ){
					$customer_log['title'] = '由' . heivrid($admincp->admins[$admincp->adminid]) . '于' . date('Y-m-d H:i:s', SYS_TIME) . '重新放入客户池';
				}else{
					$customer_log['title'] = '由' . heivrid($admincp->admins[$admincp->adminid]) . '于' . date('Y-m-d H:i:s', SYS_TIME) . '更改负责人为:' . heivrid($admincp->admins[$adminid]);
				}
			}else{
				$customer_log['title'] = '由' . heivrid($admincp->admins[$admincp->adminid]) . '于' . date('Y-m-d H:i:s', SYS_TIME) . '从客户池中认领';
			}
			DB::Insert('customer_log', $customer_log);
		}

	}else{
		$update['create_time'] = SYS_TIME;
		$update['creatorid'] = $admincp->adminid;
		$customerid = DB::Insert('customer', $update);
		$admincp->log('customer', $customerid, '添加客户：' . $name, $update);
		$customer_log['customerid'] = $customerid;
		$customer_log['title'] = '由' . heivrid($admincp->admins[$admincp->adminid]) . '于' . date('Y-m-d H:i:s', SYS_TIME) . '创建' . (!$adminid ? '并放入到客户池' : ($adminid != $admincp->adminid ? ('，由' . heivrid($admincp->admins[$adminid]) . '负责跟进') : ''));
		DB::Insert('customer_log', $customer_log);
	}
	delete('customer_demands', $customerid, 'customerid');

	foreach( $demand as $k => $v ){
		DB::Insert('customer_demands', array(
			'customerid' => $customerid,
			'key' => $k
		));
	}
	$admincp->jsonp($customerid);
	$admincp->success();
}


?>